/* 文章模块
 * @Author: Leil 
 * @Date: 2018-08-24 16:57:10 
 * @Last Modified by: Leil
 * @Last Modified time: 2018-09-06 11:40:31
 */
const Post = require('../lib/mongo').Post
const config = require('config-lite')(__dirname)

function addOnePost(data) {
    return Post
        .insertOne(data)
        .exec()
}

function getPosts() {
    return Post
        .find()
        .exec()
}

function getPostById(postId) {
    return Post
        .findOne({_id: postId})
        .exec()
}

function updatePostById(postId, data) {
    return Post
        .updateOne({_id: postId}, {$set: data})
        .exec()
}

function getPostsCount(query = {}) {
    // console.log(query)
    return Post
        .countDocuments(query)
        .exec()
}

function getPostsByCondition(query) {
    return Post
        .find(query)
        .exec()
}

function getPostsByPage(currentPageNum) {
    // console.log(`getPostsByPage.............${currentPageNum}`)
    const numPerPage = config.blogNumPrePage
    return Post
        .find()
        .skip((currentPageNum - 1) * numPerPage)
        .limit(numPerPage)
        .exec()
}

function getPostsTitleInfo(query = {}) {
    return Post
            .find(query, {title: 1})
            .exec()
}

module.exports = {
    addOnePost,
    getPosts,
    updatePostById,
    getPostById,
    getPostsCount,
    getPostsByCondition,
    getPostsByPage,
    getPostsTitleInfo
}